<script setup>
import { ref, computed, onMounted } from 'vue'
import { Tab, Tabs, Empty, Loading, Icon } from 'vant'
import axios from "@/utils/axios.js"
import {useUserStore} from "@/stores/user.js";
import {useRouter } from "vue-router";

const router = useRouter();
const users = useUserStore().user
const historyData = ref([])
const loading = ref(true)

const us = JSON.parse(sessionStorage.getItem("user"));
const init=()=>{
  axios.get(`api/browseHistory/getHistory/${us.userId}`)
      .then(res=>{
        console.log(res.data)
        historyData.value = res.data
        if(res.data.length>0){
          loading.value = false
        }
        console.log(historyData.value)
      })
}

onMounted(() => {
  init()
})

const formatTime = (timeStr) => {
  return timeStr.replace(/\s/g, ' ')
}

const goToEntry=(val)=>{
  router.push({ name: 'entryWithId', params: { entryId: val } });
}
</script>

<template>
  <van-nav-bar
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
      safe-area-inset-top
  />
  <div class="history-container">
    <div class="header">
      <h1>浏览历史</h1>
      <p>共{{ historyData.length }}条浏览记录</p>
    </div>

    <div class="history-list">
      <div v-if="loading" class="empty-state">
        <Loading size="24px">加载中...</Loading>
      </div>

      <Empty v-else-if="historyData.length === 0" description="暂无浏览记录" class="empty-state">
        <template #image>
          <Icon name="folder-o" size="60px" />
        </template>
      </Empty>

      <div v-else class="list-content">
        <div v-for="item in historyData" :key="item.id" class="history-item" @click="goToEntry(item.entryId)">
          <div class="item-icon entry">
            <Icon name="book" />
          </div>
          <div class="item-content">
            <div class="item-type">浏览了词条</div>
            <div class="item-title">{{ item.entryName }}</div>
            <div class="item-time">{{ formatTime(item.browseTime) }}</div>
            <div class="item-stats">
              <div class="stat">
                <Icon name="like-o" /> {{ item.likes }}
              </div>
              <div class="stat">
                <Icon name="star-o" /> {{ item.collects }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.history-container {
  max-width: 800px;
  margin: 0 auto;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.header {
  padding: 20px;
  background: linear-gradient(135deg, #ff9500, #ff7a00);
  color: white;
}

.header h1 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.header p {
  opacity: 0.9;
}

.history-list {
  padding: 0 20px 20px;
}

.list-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.history-item {
  display: flex;
  padding: 16px;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

.history-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.item-icon {
  width: 50px;
  height: 50px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  flex-shrink: 0;
}

.item-icon.entry {
  color: #ff9500;
  background: #fff6e6;
}

.item-content {
  flex: 1;
}

.item-type {
  font-size: 12px;
  color: #999;
  margin-bottom: 6px;
}

.item-title {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 8px;
  line-height: 1.4;
  color: #333;
}

.item-time {
  font-size: 12px;
  color: #999;
  margin-bottom: 8px;
}

.item-stats {
  display: flex;
  gap: 16px;
}

.stat {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #666;
}

.stat :deep(.van-icon) {
  margin-right: 4px;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #999;
}

.empty-state :deep(.van-icon) {
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state p {
  font-size: 16px;
}

@media (max-width: 600px) {
  .history-container {
    border-radius: 0;
    box-shadow: none;
  }

  .header {
    border-radius: 0;
  }

  .history-item {
    flex-direction: column;
    align-items: flex-start;
  }

  .item-icon {
    margin-bottom: 12px;
    margin-right: 0;
  }
}
</style>